﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoftRemoval.Model;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;

namespace SoftRemoval
{
  public class Program
  {
    public static void Main(string[] args)
    {
      // Loading domain configuration section with name "Default".
      var domainConfiguration = DomainConfiguration.Load("Default");
      var domain = Domain.Build(domainConfiguration);

      using (var session = domain.OpenSession())
      using (var tx = session.OpenTransaction()) {

        // This activates our filter applier
        session.OverrideQueryRoot(new SoftRemoveFilterApplier(session.Query));

        // Create some products and orders
        Console.WriteLine("Creating products and orders:");
        var p1 = new Product(session);
        var p2 = new Product(session);
        new Order(session, p1);
        new Order(session, p2);
        Show(session.Query);

        Console.WriteLine("Hiding some products:");
        p1.IsHidden = true;
        Show(session.Query);

        // Session.SystemQuery ignores any custom query root builders
        Console.WriteLine("Displaying objects via SystemQuery:");
        Show(session.SystemQuery);

        Console.WriteLine("Restoring hidden product:");
        p1.IsHidden = false;
        Show(session.Query);
      }
    }

    public static void Show(QueryEndpoint query)
    {
      Console.WriteLine();
      Console.WriteLine("  All visible products:");
      foreach (var product in query.All<Product>())
        Console.WriteLine("    {0}", product);
      Console.WriteLine();

      Console.WriteLine("  All visible orders:");
      foreach (var order in query.All<Order>())
        Console.WriteLine("    {0}", order);
      Console.WriteLine();
    }
  }
}
